import 'package:huxiaodui/api/api.dart';
import 'package:huxiaodui/api/model/result.dart';
import 'package:huxiaodui/api/model/unit_model.dart';
import 'package:huxiaodui/components/UnitsComponent.dart';
import 'package:flutter/material.dart';

class MyUnitsPage extends StatefulWidget {
  const MyUnitsPage({super.key});

  @override
  MyUnitsPageState createState() => MyUnitsPageState();
}

class MyUnitsPageState extends State<MyUnitsPage> {
  List<UnitModel> units = [];

  getUnits() async {
    await Api.request(
      isToast: true,
      url: MobileAppUrl.units,
      success: (Result res) {
        setState(() {
          units = UnitModel.toList(res.data);
        });
      },
    );
  }

  @override
  void initState() {
    super.initState();
    getUnits();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("我的单位"),
      ),
      body: RefreshIndicator(
        onRefresh: () async {
          await getUnits();
        },
        child: UnitsComponent(units: units),
      ),
    );
  }
}
